from django.db import models

# Create your models here.
from projects.models import Modules, Projects
from users.models import UserProfile


class ApiDefine(models.Model):
    module = models.ForeignKey(Modules, on_delete=models.CASCADE, verbose_name='所属模块')
    tester = models.ForeignKey(UserProfile, on_delete=models.CASCADE, verbose_name='负责人')
    name = models.CharField(max_length=200, verbose_name='接口名称')
    method_choice = (0, 'GET'), (1, 'POST'), (2, 'PUT'), (3, 'DELETE')
    method = models.IntegerField(choices=method_choice, verbose_name='请求方法')
    interface = models.CharField(max_length=200, verbose_name='接口路径')
    headers = models.CharField(max_length=1000, verbose_name='请求信息头')
    params = models.CharField(max_length=1000, verbose_name='params参数')
    body_type_choice = (0, 'form-data'), (1, 'x-www-form-urlencoded'), (2, 'json'), (3, 'xml')
    body_type = models.IntegerField(choices=body_type_choice, verbose_name='请求体类型')
    body = models.CharField(max_length=1000, verbose_name='请求体')
    describe = models.CharField(max_length=1000, verbose_name='接口描述')
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    status_choice = (0, '待执行'), (1, '进行中'), (2, '执行失败'), (3, '已关闭')
    status = models.IntegerField(choices=status_choice, verbose_name='接口状态')
    is_assert = models.IntegerField(default=0, verbose_name='是否断言')
    assert_type_choice = (0, '匹配'), (1, '包含'), (2, '状态码')
    assert_type = models.IntegerField(choices=assert_type_choice, verbose_name='断言类型')
    assert_body = models.CharField(max_length=2000, verbose_name='断言值')

    class Meta:
        verbose_name = '接口定义'
        verbose_name_plural = '接口定义'

    def __str__(self):
        return self.name


class CaseTemporary(models.Model):
    name = models.CharField(max_length=200, verbose_name='接口名称')
    method_choice = (0, 'GET'), (1, 'POST'), (2, 'PUT'), (3, 'DELETE')
    method = models.IntegerField(choices=method_choice, verbose_name='请求方法')
    interface = models.CharField(max_length=200, verbose_name='接口路径')
    headers = models.CharField(max_length=1000, verbose_name='请求信息头')
    params = models.CharField(max_length=1000, verbose_name='params参数')
    body_type_choice = (0, 'form-data'), (1, 'x-www-form-urlencoded'), (2, 'json'), (3, 'xml')
    body_type = models.IntegerField(choices=body_type_choice, verbose_name='请求体类型')
    body = models.CharField(max_length=1000, verbose_name='请求体')
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    is_assert = models.IntegerField(default=0, verbose_name='是否断言')
    assert_type_choice = (0, '匹配'), (1, '包含'), (2, '状态码')
    assert_type = models.IntegerField(choices=assert_type_choice, verbose_name='断言类型')
    assert_body = models.CharField(max_length=2000, verbose_name='断言值')

    class Meta:
        verbose_name = "用例暂存表"
        verbose_name_plural = '用例暂存表'

    def __str__(self):
        return self.name

class ApiCase(models.Model):
    api_define = models.ForeignKey(ApiDefine, on_delete=models.CASCADE, verbose_name='所属接口')
    name = models.CharField(max_length=200, verbose_name='接口名称')
    method_choice = (0, 'GET'), (1, 'POST'), (2, 'PUT'), (3, 'DELETE')
    method = models.IntegerField(choices=method_choice, verbose_name='请求方法')
    interface = models.CharField(max_length=200, verbose_name='接口路径')
    headers = models.CharField(max_length=1000, verbose_name='请求信息头')
    params = models.CharField(max_length=1000, verbose_name='params参数')
    body_type_choice = (0, 'form-data'), (1, 'x-www-form-urlencoded'), (2, 'json'), (3, 'xml')
    body_type = models.IntegerField(choices=body_type_choice, verbose_name='请求体类型')
    body = models.CharField(max_length=1000, verbose_name='请求体')
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    is_assert = models.IntegerField(default=0, verbose_name='是否断言')
    assert_type_choice = (0, '匹配'), (1, '包含'), (2, '状态码')
    assert_type = models.IntegerField(choices=assert_type_choice, verbose_name='断言类型')
    assert_body = models.CharField(max_length=2000, verbose_name='断言值')


    class Meta:
        verbose_name = '接口用例'
        verbose_name_plural = '接口用例'

    def __str__(self):
        return self.name


class ApiAutoTest(models.Model):
    project = models.ForeignKey(Projects, on_delete=models.CASCADE, verbose_name="所属项目")
    name = models.CharField(max_length=200, verbose_name='用例名称')
    priority_choice = (0, 'P0'), (1, 'P1'), (2, 'P2'), (3, 'P3')
    priority = models.IntegerField(choices=priority_choice, verbose_name='优先级')
    creator = models.CharField(max_length=200, verbose_name='创建人')
    describe = models.TextField(max_length=200, verbose_name='说明', null=True)
    u_creator = models.CharField(max_length=200, verbose_name='修改者', default="")
    cases = models.CharField(max_length=200, verbose_name='关联用例', default="")
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)

    class Meta:
        verbose_name = '测试用例'
        verbose_name_plural = '测试用例'

    def __str__(self):
        return self.name
